package com.shopping.util;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.shopping.bean.User;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

public class SYS {
    public static  final String FILE_UPLOAD_DIR="D:\\easyupload\\";
    public static final String FILE_UPLOAD_URL="easyfile";

    public static final String SHOPPING_LOGIN_USER=" SHOPPING_LOGIN_USER";
    public static final String SYSTEM_LOGIN_ADMIN=" SYSTEM_LOGIN_ADMIN";

    public static  int getLoginUserID(HttpSession session){
        User user=(User) session.getAttribute(SHOPPING_LOGIN_USER);
        return user.getUserid();
    }

    public static final int ADDREE_MAX_NUMBER=6;

    public static final int[] HEX_CHAR_ARR={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

    public static boolean check(String reg,String... params){
        boolean result=true;
        for (String item:params){
            if (item==null){
                return false;
            }
            if (!item.matches(reg)){
                return false;
            }
        }
        return result;
    }

    public static String digest(String pass) {

//随机次数随机盐值加密
        int countIndex = (int) (Math.random() * HEX_CHAR_ARR.length);
        int saltIndex = (int) (Math.random() * HEX_CHAR_ARR.length);
        int count = HEX_CHAR_ARR[countIndex];
        char salt = (char) HEX_CHAR_ARR[saltIndex];
        char countChar = (char) count;
        pass=pass+salt;

        for (int i=0;i<count;i++){
            pass= DigestUtils.md5DigestAsHex(pass.getBytes(StandardCharsets.UTF_8));

        }

        String passwordText=salt+pass+countChar;

return passwordText;
    }
    //登录验证方法
    public static  boolean checkPass(String pass,String hexPass){
        char salt=hexPass.charAt(0);//盐值
        char countChar=hexPass.charAt(hexPass.length()-1);//次数
        pass=pass+salt;
        for (int i=0;i<countChar;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes(StandardCharsets.UTF_8));
        }
        String text=salt+pass+countChar;
        return  text.equals(hexPass);
    }

}
